package com.abbas.smartCity.ui.youthInn;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.abbas.smartCity.App;
import com.abbas.smartCity.R;
import com.abbas.smartCity.adapter.InnExpendableAdapter;
import com.abbas.smartCity.adapter.ListCommAdapter;
import com.abbas.smartCity.base.BaseActivity;
import com.abbas.smartCity.bean.inn.InnListBean;
import com.abbas.smartCity.bean.inn.TalentPolicyAreaListBean;
import com.abbas.smartCity.retroft.ICallback;
import com.abbas.smartCity.view.MExpandableListView;
import com.abbas.smartCity.view.MGridView;
import com.bumptech.glide.Glide;

import java.util.List;

import retrofit2.Call;

public class YouthInnMainActivity extends BaseActivity {

    private MGridView gv;
    private MExpandableListView exList;
    private List<InnListBean.RowsBean> rows;
    private List<TalentPolicyAreaListBean.DataBean> dataBeanList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_youth_inn_main;
    }

    @Override
    protected String setCustomTitle() {
        return "青年驿站";
    }

    @Override
    protected void initView() {
        gv = (MGridView) findViewById(R.id.gv);
        exList = (MExpandableListView) findViewById(R.id.ex_list);
    }

    @Override
    protected void initData() {
        api.getTalentPolicyAreaList().enqueue(new ICallback<TalentPolicyAreaListBean>() {
            @Override
            public void onSuccess(Call<TalentPolicyAreaListBean> call, TalentPolicyAreaListBean data) {
                dataBeanList = data.getData();
                gv.setAdapter(new ListCommAdapter<TalentPolicyAreaListBean.DataBean,ListCommAdapter.YouthInnGvHolder>(
                        dataBeanList,
                        ListCommAdapter.YouthInnGvHolder.class,
                        R.layout.youth_inn_gv_item
                ) {
                    @Override
                    protected void show(TalentPolicyAreaListBean.DataBean data, View convertView, YouthInnGvHolder viewHolder, int position, Context context) {
                        Glide.with(context)
                                .load(App.getBaseUrl() + data.getImgUrl())
                                .into(viewHolder.im);
                    }
                });
            }
        });

        api.getInnList().enqueue(new ICallback<InnListBean>() {
            @Override
            public void onSuccess(Call<InnListBean> call, InnListBean data) {
                rows = data.getRows();
                exList.setAdapter(new InnExpendableAdapter(rows, (i, b) -> {
                    if (b) {
                        exList.collapseGroup(i);
                    } else {
                        exList.expandGroup(i);
                    }
                }));

            }
        });

    }

    @Override
    protected void initEvent() {
        exList.setOnGroupClickListener((expandableListView, view, i, l) -> {
            final Intent intent = new Intent(YouthInnMainActivity.this, InnDetailActivity.class);
            intent.putExtra("id",rows.get(i).getId());
            startActivity(intent);
            return true;
        });

        gv.setOnItemClickListener((adapterView, view, i, l) -> {
            final Intent intent = new Intent(YouthInnMainActivity.this, TalentPeoplePolicyActivity.class);
            intent.putExtra("id",dataBeanList.get(i).getId());
            startActivity(intent);
        });
    }
}